package com.dingding.service.account.core.dao.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Userintegraldetail;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.account.integral.enums.IntegralTradeTypeEnum;
import com.dingding.service.account.core.dao.IUserIntegralDetailDao;
import com.dingding.service.account.core.dao.sql.UserIntegralDetailSQL;

/**
 * 
 * @ClassName: UserIntegralDetailDaoImpl 
 * @Description: 用户积分交易明细表DAO
 * @author CHEHAUNBO 
 * @date 2016年3月30日 下午4:18:09
 * 
 */
@Component("userIntegralDetailDao")
public class UserIntegralDetailDaoImpl extends BaseDaoImpl<Userintegraldetail> implements IUserIntegralDetailDao {

	private Logger log = Logger.getLogger(UserIntegralDetailDaoImpl.class);

	/**
	 * 
	 * @Title: addUserIntegralDetail 
	 * @Description:添加用户积分交易明细
	 * @param userId 用户ID
	 * @param tradeMoney  交易金额
	 * @param tradeType 交易类型
	 * @return boolean true 交易成功 false 交易失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUserIntegralDetail(String userId, BigDecimal tradeMoney, IntegralTradeTypeEnum tradeType,
			String tradeContent) {

		Userintegraldetail userintegraldetail = new Userintegraldetail();
		// 交易明细主键ID
		userintegraldetail.setUadId(ToolUtil.getUid());
		// 用户ID
		userintegraldetail.setUserId(userId);
		// 积分交易类型
		userintegraldetail.setTradeType(tradeType.getIndex());
		// 交易时间
		userintegraldetail.setTradeTime(new Date());
		// 赠送积分
		userintegraldetail.setTradeMoney(tradeMoney);
		// 交易内容
		userintegraldetail.setTradeContent(tradeContent);

		try {

			super.save(userintegraldetail);
			return true;

		} catch (Exception e) {
			log.error("添加积分交易明细出现异常！", e);
		}

		return false;
	}

	/**
	 * 
	 * @Title: getAllUserIntegralDetail 
	 * @Description:获取所有的积分交易明细
	 * @param userId 用户ID
	 * @param rows 当前页
	 * @param pageSize 每页显示记录数
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public List<Userintegraldetail> getUserIntegralDetail(String userId, int rows, int pageSize) {

		List<Userintegraldetail> userintegraldetail = null;
		try {
			userintegraldetail = super.listHqlByPage(UserIntegralDetailSQL.GET_USERINTEGRALDETAIL_BY_USERID, rows,
					pageSize, userId);
		} catch (Exception e) {
			log.error("获取积分交易明细出现异常！", e);
		}

		return userintegraldetail;
	}

}
